package com.athena.dal.entities;

import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;


/**
 * The persistent class for the examination database table.
 * 
 */
@Entity
@Table(name="examination")
@NamedQueries({
	@NamedQuery(name="Examination.findForSubject", query="SELECT exam FROM Examination exam WHERE exam.subjectId = :subjectId ORDER BY exam.id ASC "),
	@NamedQuery(name="Examination.findForFaculty", query="SELECT exam FROM Examination exam WHERE exam.examinerFacultyId = :facultyId ORDER BY exam.subjectId ASC, exam.id ASC ") /*,
	@NamedQuery(name="Examination.findForClass",   query="SELECT exam FROM Examination exam JOIN Subject subject ON subject.id = exam.subjectId WHERE subject.classId = :classId ORDER BY exam.subjectId ASC, exam.id ASC ")*/
})
public class Examination implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	@Column(unique=true, nullable=false)
	private int id;

	@Column(nullable=false)
	private Time duration;

	private int examinerFacultyId;

	@Column(precision=10)
	private BigDecimal fullMarks;

	@Column(precision=10, scale=2)
	private BigDecimal passingMarks;

	@Column(length=1)
	private String state;

	@Column(nullable=false)
	private int subjectId;

	@Column(nullable=false, length=1)
	private String type;

	@Column(nullable=false)
	private Timestamp updatedAt;

	private int weightage;

	//bi-directional many-to-one association to ExaminationSchedule
	@OneToMany(mappedBy="examination")
	private Set<ExaminationSchedule> examinationSchedules;

	//bi-directional many-to-one association to Mark
	@OneToMany(mappedBy="examination")
	private Set<Marks> marks;

	public Examination() {
	}
	
	/**
	 * @param subjectId2
	 * @param minutes
	 * @param examType
	 */
	public Examination(int subjectId, int minutes, ExamType examType) {
		this.subjectId = subjectId;
		this.duration  = new Time(minutes);
		setType(examType);
	}

	public enum ExamType {
		UNITTEST("UNIT TEST"),
		MIDTERM("MID TERM"),
		FINAL("FINAL");
		
		private final String value;
		
		private ExamType(String value) {
			this.value = value;
		}
		
		public static ExamType get(String value){
			if(value == null) { return null; }
			if(UNITTEST.value.equalsIgnoreCase(value)){
				return UNITTEST;
			}
			if(MIDTERM.value.equalsIgnoreCase(value)){
				return MIDTERM;
			}
			if(FINAL.value.equalsIgnoreCase(value)){
				return FINAL;
			}
			return null;
		}
		
		public String value(){
			return value;
		}
	}

	public int getId() {
		return this.id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Time getDuration() {
		return this.duration;
	}

	public void setDuration(Time duration) {
		this.duration = duration;
	}

	public int getExaminerFacultyId() {
		return this.examinerFacultyId;
	}

	public void setExaminerFacultyId(int examinerFacultyId) {
		this.examinerFacultyId = examinerFacultyId;
	}

	public BigDecimal getFullMarks() {
		return this.fullMarks;
	}

	public void setFullMarks(BigDecimal fullMarks) {
		this.fullMarks = fullMarks;
	}

	public BigDecimal getPassingMarks() {
		return this.passingMarks;
	}

	public void setPassingMarks(BigDecimal passingMarks) {
		this.passingMarks = passingMarks;
	}

	public String getState() {
		return this.state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public int getSubjectId() {
		return this.subjectId;
	}

	public void setSubjectId(int subjectCode) {
		this.subjectId = subjectCode;
	}

	/*public String getType() {
		return this.type;
	}*/

	protected void setType(String type) {
		this.type = type;
	}
	
	public ExamType getType() {
		return ExamType.get(type);
	}

	public void setType(ExamType type) {
		if(type == null) {
			this.type = null;
		}
		this.type = type.value();
	}

	public Timestamp getUpdatedAt() {
		return this.updatedAt;
	}

	public void setUpdatedAt(Timestamp updatedAt) {
		this.updatedAt = updatedAt;
	}

	public int getWeightage() {
		return this.weightage;
	}

	public void setWeightage(int weightage) {
		this.weightage = weightage;
	}

	public Set<ExaminationSchedule> getExaminationSchedules() {
		return this.examinationSchedules;
	}

	public void setExaminationSchedules(Set<ExaminationSchedule> examinationSchedules) {
		this.examinationSchedules = examinationSchedules;
	}

	public Set<Marks> getMarks() {
		return this.marks;
	}

	public void setMarks(Set<Marks> marks) {
		this.marks = marks;
	}

}